<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<h1>发布微博页面</h1>
<div id="app">
    <input type="text" v-model="weibo.content" placeholder="说点儿什么...">
    <!-- limit = "1" 设置只能选择上传一张图片 -->
    <el-upload
            action="/upload"
            name="picFile"
            :limit="1"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <input type="button" value="发布微博" @click="insert()">
</div>
</body>

<script src="js/vue.js"></script>
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo: {
                    content:"",
                    url:""
                }
            }
        },
        methods: {
            handleRemove(file, fileList) {
                //当点击删除图片时方法会执行
                //file代表要删除的文件,file.response代表文件上传成功后,服务器响应的数据
                console.log("文件名:"+file.response)
                //发出删除服务器本地的图片的请求
                axios.get("/remove?name="+file.response).then(function (response) {
                     console.log("服务器图片删除完成!");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert() {
                //得到用户输入的微博文本内容和图片名,一起提交给服务器
                if (v.weibo.content.trim() == "" || v.weibo.url == "") {
                    alert("微博内容或图片不能为空!");
                    return;
                }
                axios.post("/insert",v.weibo).then(function (response) {
                    if (response.data == 1) {
                        alert("添加完成!");
                        location.href="/"; //回到首页
                    }else {
                        alert("请先登录!");
                        location.href="/login.html"; //显示登录页面
                    }

                })
            },
            handleSuccess(response, file, fileList) {
                //response = file.response;
                console.log("文件上传完成,图片名 = " + response);
                v.weibo.url = response;
            }
        }
    })
</script>
</html>